package org.dut.mybatis.Mapper;

import org.apache.ibatis.annotations.*;
import org.dut.mybatis.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    //观察 # 取值和 $ 取值的区别
    @ResultMap(value = "resultMap")
    @Select("select * from userinfo where id = #{id}")
    List<UserInfo> selectUserInfo(@Param("id") Integer id);

    //SQL注入
    @Select("select * from userinfo where username = '${username}' and password ='${password}'")
    List<UserInfo> selectByPassword(UserInfo userInfo);

    //应对名称转换：起别名，结果映射，驼峰自动转换
    //结果映射
    @Select("select * from userinfo")
    @Results(id="resultMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    List<UserInfo> selectUserInfoList();

    //获取主键，赋值给id
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into userinfo(username,`password`,age,gender) values " +
            "(#{username},#{password},#{age},#{gender})")
    Integer insertUserInfo(UserInfo userInfo);

    // @Param 将对象视为一个整体
    @Insert("insert into userinfo(username,`password`,age,gender) values " +
            "(#{userInfo.username},#{userInfo.password},#{userInfo.age},#{userInfo.gender})")
    Integer insertUserInfo2(@Param("userInfo") UserInfo userInfo);


    //可以使用param1，param2传参，不直观
    @Update("update userinfo set username= #{username} where id = #{id}")
    Integer updateUserInfo(String username, Integer id);
//    Integer updateUserInfo(UserInfo userInfo);

    @Delete("delete from userinfo where id =#{id}")
    Integer deleteUserInfo(Integer id);

    //避免使用$，使用concat拼接
    @Select("select * from userinfo where username like concat('%',#{key},'%')")
    List<UserInfo> queryAllUserByLike(String key);



}
